package com.example.mysdktest.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.mysdktest.R;

import java.util.LinkedList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by ant on 2016/11/24.
 */

public class CarouselfigureView extends ViewPager {
    private static final int SEND_WHAT = 1023;
    public static long delTime = 1000;//切换动画时间间隔

    public List<View> blist;

    private MyPagerAdapter adapter;
    private int mposition = 0;

    private Handler hand = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SEND_WHAT:
                    CarouselfigureView.this.setCurrentItem(mposition);
                    break;
            }
        }


    };

    public CarouselfigureView(Context context) {
        super(context);
        initThisView(context, null);
    }

    public CarouselfigureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initThisView(context, attrs);
    }


    private void initThisView(Context context, AttributeSet attrs) {
        blist = new LinkedList<>();

        adapter = new MyPagerAdapter();
        this.setAdapter(adapter);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = SEND_WHAT;
                hand.sendMessageDelayed(message, delTime);
            }
        }, 1000, 1500);
    }

    public void addItem(Bitmap bitmap) {
        if (null != bitmap)
            blist.add(newItem(bitmap));
        if (adapter != null)
            adapter.upData();
    }

    public void removeItem(int index) {
        if (index >= 0 && index < blist.size()) {
            blist.remove(index);
            if (adapter != null)
                adapter.upData();
        }
    }


    public View newItem(Bitmap bitmap) {
        if (bitmap == null)
            return null;
        View img = LayoutInflater.from(getContext()).inflate(R.layout.view_carouselfigure_iteam_layout, null);
        ((ImageView) img.findViewById(R.id.view_carouselfigure_iteam_layout_img01)).setImageBitmap(bitmap);
        return img;
    }


    class MyPagerAdapter extends PagerAdapter {

        public void upData() {
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return blist == null ? 0 : Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            mposition = position;
            container.addView(blist.get(position % blist.size()));
            return blist.get(position % blist.size());
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(blist.get(position % blist.size()));
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //这里去掉了ViewPager的滑动事件
        return true;
    }
}
